<?php
require_once dirname(__FILE__) . '/../staff/StaffView.php';

class EventView {
	private static $k_title = 'title';
	private static $k_starttime = 'starttime';
	private static $k_endtime = 'endtime';
	private static $k_startdate = 'startdate';
	private static $k_enddate = 'enddate';
	private static $k_number_of_staff = 'number_of_staff';
	private static $k_number_of_cooks = 'number_of_cooks';
	private static $k_description = 'desctiption';	
	private static $k_submit_event = 'submit_event';
	private static $k_staff_event_id = 'staff_event_id';
	private static $k_visitor_event_id = 'visitor_event_id';
	private static $k_submit_form_visitor_event_id = 'submit_form_visitor_event_id';
	private static $k_submit_form_staff_event_id = 'submit_form_staff_event_id';
	private static $k_delete_event = 'delete_event';
	private static $k_edit_event = 'edit_event';
	private static $k_apply_as_staff = 'apply_as_staff';
	private static $k_staff_role = 'staff_role';


	private static $dateFormat = 'Y-m-d\TH:i:s';
	
	/**
	* Formulär för att lägga till/uppdatera event. Kommer det ett event med är det en uppdatering,
	* annars är det en tilläggning.
	*
	* @param EventPair (optional)
	*
	* @return String HTML
	*/
	public function getEventForm(EventPair $event = null) {
		if ($event) {
			$formTitle = 'Redigera event';
			$title = $event->visitorEvent->getTitle();
			$startTime = $event->visitorEvent->getStartTime();
			$endTime = $event->visitorEvent->getEndTime();
			$startDate = $event->visitorEvent->getStartDate();
			$endDate = $event->visitorEvent->getEndDate();
			$description = $event->visitorEvent->getDescription();
			$visitorEventId = $event->visitorEvent->getId();
			$staffEventId = $event->staffEvent->getId();
		} else {
			$formTitle = 'Lägg till event';
			$title = 'Pubkväll!';
			$startTime = '19:00';
			$endTime = '01:00';
			$startDate = '';
			$endDate = '';
			$description = '';
			$visitorEventId = '';
			$staffEventId = '';
		}
		
		return '<div id="event-form">
					<form method="post">
						<h2>' . $formTitle . '</h2>
						<div class="input">
							<label for="' . self::$k_title . '">Titel: </label>
							<input name="' . self::$k_title . '" id="' . self::$k_title . '" value="' . $title . '" />
						</div>
						<div class="input">
							<label for="' . self::$k_starttime . '">Starttid: </label>
							<input name="' . self::$k_starttime . '" id="' . self::$k_starttime . '" value="' . $startTime . '" />
							<label for="' . self::$k_startdate . '">Datum: </label>
							<input name="' . self::$k_startdate . '" id="' . self::$k_startdate . '" value="' . $startDate . '" />
						</div>
						<div class="input">
							<label for="' . self::$k_endtime . '">Sluttid: </label>
							<input name="' . self::$k_endtime . '" id="' . self::$k_endtime . '" value="' . $endTime . '" />
							<label for="' . self::$k_enddate . '">Datum: </label>
							<input name="' . self::$k_enddate . '" id="' . self::$k_enddate . '" value="' . $endDate . '" />
						</div>
						<div class="input">
							<label for="' . self::$k_number_of_staff . '">Antal jobbare: </label>
							<input name="' . self::$k_number_of_staff . '" id="' . self::$k_number_of_staff . '" value="5" />
						</div>
						<div class="input">
							<label for="' . self::$k_number_of_cooks . '">Antal kockar: </label>
							<input name="' . self::$k_number_of_cooks . '" id="' . self::$k_number_of_cooks . '" value="2" />
						</div>
						<div class="input">
							<label for="' . self::$k_description . '">Beskrivning: </label>
							<input name="' . self::$k_description . '" id="' . self::$k_description . '" value="' . $description . '" />
						</div>
						<input type="hidden" name="' . self::$k_submit_form_visitor_event_id . '" value="' . $visitorEventId . '" />
						<input type="hidden" name="' . self::$k_submit_form_staff_event_id . '" value="' . $staffEventId . '" />
						<input type="submit" name="' . self::$k_submit_event . '" value="Skicka!" />
					</form>
				</div>';
	}

	/**
	* Hämtar'objekt med informationen användaren matat in
	*
	* @return EventPair
	*/
	public function getEventInput() {
		$visitorEventId = $this->getValue(self::$k_submit_form_visitor_event_id);
		$staffEventId = $this->getValue(self::$k_submit_form_staff_event_id);
		$title = $this->getValue(self::$k_title);
		$starttime = $this->getValue(self::$k_starttime);
		$endtime = $this->getValue(self::$k_endtime);
		$startdate = $this->getValue(self::$k_startdate);
		$enddate = $this->getValue(self::$k_enddate);
		$number_of_staff = $this->getValue(self::$k_number_of_staff);
		$number_of_cooks = $this->getValue(self::$k_number_of_cooks);
		$description = $this->getValue(self::$k_description);

		$start = new DateTime($startdate . ' ' . $starttime);
		$end = new DateTime($enddate . ' ' . $endtime);
		$startDateString = $start->format(self::$dateFormat);
		$endDateString = $end->format(self::$dateFormat);

		$staffStart = $start;
		$staffStart->modify('-60 minutes');

		$staffEnd = $end;
		$staffEnd->modify('+60 minutes');
		$staffStartDateString = $staffStart->format(self::$dateFormat);
		$staffEndDateString = $staffEnd->format(self::$dateFormat);

		$visitorEvent = VisitorEvent::create($visitorEventId, $title, $description, $startDateString, $endDateString);

		// TODO Ta ställning till om detta ska finnas kvar
		$staffInfo = '{"workers": [
	{"role" : "jobbare", "amount" : ' . $number_of_staff . ' },
	{"role" : "kock", "amount" : ' . $number_of_cooks . ' }
]}';	

		$staffEvent = StaffEvent::create($staffEventId, $title . ' - jobbare', $staffInfo, $staffStartDateString, $staffEndDateString, null);
		return EventPair::create($visitorEvent, $staffEvent);		
	}

	/**
	* Hjälpfunktion för att hämta vardet på plats $key i $_POST
	*
	* @param String
	*
	* @return String (null om $key inte är satt)
	*/
	private function getValue($key) {
		if (isset($_POST[$key])) {
			return $_POST[$key];
		}
		return null;
	}

	/**
	* Hämtar html-beskrivning av de event som skickas med
	*
	* @param array EventPair
	*
	* @return String HTML
	*/
	public function getEventsMarkup(/*array() EventPair*/ $events) {
		$html = '';
		foreach ($events as $event) {
			$html .= $this->getEventDescriptionMarkup($event->visitorEvent);
		}

		return $html;
	}

	/**
	* Skriver ut event information om och möjlighet att anmäla intresse till att jobba på varje event
	* i EventPair-arrayen
	*
	* @param array EventPair
	*
	* @return String HTML
	*/
	public function getStaffEventsMarkup(/*array() EventPair*/ $events) {
		$html = '';
		foreach ($events as $event) {
			$html .= $this->getStaffEventMarkup($event);
		}

		return $html;
	}

	/**
	* Hämtar markap för redigering/borttagning av de event som skickas med
	*
	* @param array EventPair
	*
	* @return String HTML
	*/
	public function getAdminEventsMarkup(/*array() EventPair*/ $events) {
		$html = '';
		foreach ($events as $event) {
			$html .= $this->getAdminEventMarkup($event);
		}

		return $html;
	}

	/**
	* Hjälpfunktion för att skriva ut redigerings/borttagnings-val för event
	*
	* @param EventPair
	*
	* @return String HTML
	*/
	private function getAdminEventMarkup(EventPair $event) {
		$html = '';
		$html .= $this->getEventDescriptionMarkup($event->visitorEvent);

		$html .= '<form  method="post" class="administer-event">
					<input type="hidden" name="' . self::$k_visitor_event_id . '" value="' . $event->visitorEvent->getId() . '" />
					<input type="submit" name="' . self::$k_edit_event . '" value="Redigera!" />
					<input type="submit" name="' . self::$k_delete_event . '" class="' . self::$k_delete_event . '" value="Ta bort!" />
				</form>';

		return $html;
	}

	public function triedToSubmitEvent() {
		return isset($_POST[self::$k_submit_event]);
	}

	public function triedToDeleteEvent() {
		return isset($_POST[self::$k_delete_event]);
	}

	public function triedToEditEvent() {
		return isset($_POST[self::$k_edit_event]);
	}

	public function triedToApplyToEvent() {
		return isset($_POST[self::$k_apply_as_staff]);
	}

	public function getEventId() {
		if (isset($_POST[self::$k_visitor_event_id])) {
			return $_POST[self::$k_visitor_event_id];
		}
		return null;
	}
	
	private function getStaffEventMarkup(EventPair $event) {
		$html = '';
		$html .= $this->getEventDescriptionMarkup($event->visitorEvent);

		$html .= $this->getApplyToEventMarkup($event);
		
		return $html;
	}

	/**
	* Formulär för att anmäla intresse för att jobba på event
	*
	* @param EventPair
	*
	* @return String HTML
	*/
	private function getApplyToEventMarkup(EventPair $event) {
		$html = '';
		$html .= '<form  method="post" class="apply-to-event">
					<div class="input">
						<strong>Anmäl intressa för att jobba</strong><br />
						<select name="' . self::$k_staff_role . '" id="' . self::$k_staff_role . '">
							<option value="none_selected">Jobba son...</option>';

		foreach (StaffView::$staffRoles as $sr) {
			$html .= '<option value="' . $sr . '">' . $sr . '</option>';
		}

		$html .= '</select></div>';
		$html .= '<input type="hidden" name="' . self::$k_visitor_event_id . '" value="' . $event->visitorEvent->getId() . '" />
				<input type="submit" name="' . self::$k_apply_as_staff . '" class="' . self::$k_apply_as_staff . '" value="Anmäl intresse!" />
			</form>';

		return $html;
	}

	/**
	* Hämtar information om event som användaren vill registrera sig till
	*
	* @return EventStaff
	*/
	public function getEventApplicationInput() {
		$visitorEventId = $this->getValue(self::$k_visitor_event_id);
		$staffRole = $this->getValue(self::$k_staff_role);

		$eventStaff = EventStaff::createFromUserInput($visitorEventId, $staffRole);
		
		return $eventStaff;
	}

	/**
	* @param VisitorEvent
	*
	* @return String HTML
	*/
	private function getEventDescriptionMarkup(VisitorEvent $event) {
		return '<div class="event-description">
				<h3>' . $event->getTitle() . '</h3>
				<p>
					' . $event->getDescription() . '
				</p>
				<p>Starttid: <span class="date">' . $event->getStartDate() . ' kl ' . $event->getStartTime() . '</span></p>
				<p>Sluttid: <span class="date">' . $event->getEndDate() . ' kl ' . $event->getEndTime() . '</span></p>
			</div>';
	}
}
